from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import uvicorn
from DataAnalysis import DataAnalysis

app = FastAPI()
DA = DataAnalysis()


@app.get("/PaintModelFit/")
async def paint_model_fit(column='死亡人数', state='Washington', model=1, degree=3, alpha=0.1, add_random='false'):
    # http://127.0.0.1:8000/PaintModelFit/?column=确诊人数&state=Washington&model=4&degree=3&alpha=0.1
    if add_random == 'false':
        _add_random = False
    else:
        _add_random = True
    try:
        f = DA.paint_model_fit(column, state, int(model), int(degree), float(alpha), _add_random)
    except Exception as e:
        print(e)
        f = open('pic/Error.png', mode='rb')
    return StreamingResponse(f, media_type="image/png")


@app.get("/static/{name}")
async def say_hello(name: str):
    # http://127.0.0.1:8000/static/pic123
    if name == 'pic1' or name == 'pic2' or name == 'pic3':
        f = open('./pretreatment_pic/' + name + '.png', 'rb')
        return StreamingResponse(f)


if __name__ == '__main__':
    uvicorn.run(app="main:app", host="127.0.0.1", port=10001, reload=True)
